//
//  XinLvTestResultViewController.m
//  安好
//
//  Created by qinlongqing on 14-9-26.
//  Copyright (c) 2014年 冯凯. All rights reserved.
//

#import "XinLvTestResultViewController.h"
#import "AppConfig.h"

@interface XinLvTestResultViewController ()
{
    UIImage *_imageV;
    
    UIImageView *levelLineImageView;
    
    CGFloat nameAndFenShuLabelHeight;
    CGFloat levelViewWidth;
    CGFloat levelViewHeight;
    CGFloat jieGuoAndPingYuViewWidth;
    CGFloat jieGuoAndPingYuViewHeight;
    CGFloat dateAndSealViewHeight;
    CGFloat shareBTNHeight;
}
@end

@implementation XinLvTestResultViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"测试报告";
    
    self.view.backgroundColor = UIColorFromRGB(0xe7f1f4);
    
    [self makeNavBar];
    
    if (kScreenWidth == 414) {
        nameAndFenShuLabelHeight = 36;
        
        levelViewWidth = 348;
        levelViewHeight = 72;
        
        jieGuoAndPingYuViewWidth = 348;
        jieGuoAndPingYuViewHeight = 162;
        
        dateAndSealViewHeight = 96;
        
        shareBTNHeight = 54;
    }
    else{
        nameAndFenShuLabelHeight = 30;
        
        levelViewWidth = 290;
        levelViewHeight = 60;
        
        jieGuoAndPingYuViewWidth = 290;
        jieGuoAndPingYuViewHeight = 135;
        
        dateAndSealViewHeight = 80;
        
        shareBTNHeight = 45;
    }
    CGFloat space = (kScreenHeight - nameAndFenShuLabelHeight - levelViewHeight - jieGuoAndPingYuViewHeight - dateAndSealViewHeight - shareBTNHeight - 64)/6;

    
    
    
//    self.testResultDict = [[NSMutableDictionary alloc]init];
//    [self.testResultDict setObject:[NSNumber numberWithInt:70] forKey:kXinLvTestResult];//......................
//    [self.testResultDict setObject:@"小三" forKey:@"testUserName"];
    
    
    
    
    
    UILabel *nameAndFenShuLabel = [[UILabel alloc]init];
    nameAndFenShuLabel.frame = CGRectMake(0, space, kScreenWidth, nameAndFenShuLabelHeight);
    if (kScreenWidth == 414) {
        nameAndFenShuLabel.font = [UIFont systemFontOfSize:17];
    }
    else{
        nameAndFenShuLabel.font = [UIFont systemFontOfSize:15];
    }
    nameAndFenShuLabel.textAlignment = NSTextAlignmentCenter;
    nameAndFenShuLabel.textColor = [UIColor blackColor];
    nameAndFenShuLabel.adjustsFontSizeToFitWidth = YES;
    nameAndFenShuLabel.backgroundColor = [UIColor clearColor];
    NSString *xinLvTestResult = [self.testResultDict valueForKey:kXinLvTestResult];
    NSString *showStr = [NSString stringWithFormat:@"%@的小心脏每分钟跳动:%@次",[self.testResultDict valueForKey:@"testUserName"],xinLvTestResult];
    NSRange xinLvRange = [showStr rangeOfString:@"的小心脏每分钟跳动:"];
    NSRange ciWord = [showStr rangeOfString:@"次"];
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:showStr];
    [attributedString addAttribute:NSForegroundColorAttributeName value:UIColorFromRGB(0x4fc1e9) range:NSMakeRange(xinLvRange.location+xinLvRange.length, showStr.length - xinLvRange.location - xinLvRange.length - ciWord.length)];
    nameAndFenShuLabel.attributedText = attributedString;
    [self.view addSubview:nameAndFenShuLabel];
    
//-------------------------------------------
    UIView *levelView = [[UIView alloc]init];
    levelView.frame = CGRectMake((kScreenWidth - levelViewWidth)/2, nameAndFenShuLabel.frame.origin.y + nameAndFenShuLabel.frame.size.height + space,  levelViewWidth, levelViewHeight);
    levelView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:levelView];
    
    levelLineImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"xin_lv_test_result_level_Line"]];//290*48
    if (kScreenWidth == 414) {
        levelLineImageView.frame = CGRectMake(0, 12, levelViewWidth, 58);//1.2倍
    }
    else{
        levelLineImageView.frame = CGRectMake(0, 10, levelViewWidth, 48);
    }
    [levelView addSubview:levelLineImageView];
    
    UIImageView *positionImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"xin_lv_test_result_level_flag"]];
    CGFloat positionImageView_X = [self getPositionImageView_X];
    if (kScreenWidth == 414) {
        positionImageView.frame = CGRectMake(positionImageView_X, 6, 3, 15);
    }
    else{
        positionImageView.frame = CGRectMake(positionImageView_X, 3, 3, 15);
    }
    [levelView addSubview:positionImageView];
    
//-------------------------------------------
    UIView *jieGuoAndPingYuView = [[UIView alloc]initWithFrame:CGRectMake((kScreenWidth - jieGuoAndPingYuViewWidth)/2, levelView.frame.origin.y + levelView.frame.size.height + space, jieGuoAndPingYuViewWidth, jieGuoAndPingYuViewHeight)];
    jieGuoAndPingYuView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:jieGuoAndPingYuView];
    
    UIImageView *tiShiBG = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"test_result_fen_xiang_bg"]];
    tiShiBG.frame = CGRectMake(0, 0, jieGuoAndPingYuView.frame.size.width, jieGuoAndPingYuView.frame.size.height);
    [jieGuoAndPingYuView addSubview:tiShiBG];
    
    UILabel *jieGuoLabel = [[UILabel alloc]init];
    if (kScreenWidth == 414) {
        jieGuoLabel.frame = CGRectMake(24, 20, jieGuoAndPingYuView.frame.size.width - 48, 24);
        jieGuoLabel.font = [UIFont systemFontOfSize:15];
    }
    else{
        jieGuoLabel.frame = CGRectMake(20, 17, jieGuoAndPingYuView.frame.size.width - 40, 20);
        jieGuoLabel.font = [UIFont systemFontOfSize:13];
    }
    jieGuoLabel.text = [AppConfig getXinLvTestResult:[[self.testResultDict valueForKey:kXinLvTestResult] intValue]];
    jieGuoLabel.backgroundColor = [UIColor clearColor];
    jieGuoLabel.textAlignment = NSTextAlignmentCenter;
    jieGuoLabel.textColor = UIColorFromRGB(0x4fc1e9);
    [jieGuoAndPingYuView addSubview:jieGuoLabel];
    
    UILabel *pingYuLabel = [[UILabel alloc]init];
    pingYuLabel.text = [AppConfig getXinLvPingYuWithTestResult:[[self.testResultDict valueForKey:kXinLvTestResult] intValue]];
    pingYuLabel.backgroundColor = [UIColor clearColor];
    pingYuLabel.textAlignment = NSTextAlignmentLeft;
    pingYuLabel.textColor = UIColorFromRGB(0x4fc1e9);
    pingYuLabel.lineBreakMode = NSLineBreakByCharWrapping;
    pingYuLabel.numberOfLines = 50;
    if (kScreenWidth == 414) {
        pingYuLabel.font = [UIFont systemFontOfSize:15];
        CGSize size = [pingYuLabel.text sizeWithFont:[UIFont systemFontOfSize:15] constrainedToSize:CGSizeMake(jieGuoAndPingYuView.frame.size.width - 30, 99999) lineBreakMode:NSLineBreakByCharWrapping];
        pingYuLabel.frame = CGRectMake(20, 52, jieGuoAndPingYuView.frame.size.width - 40, size.height);
    }
    else{
        pingYuLabel.font = [UIFont systemFontOfSize:13];
        CGSize size = [pingYuLabel.text sizeWithFont:[UIFont systemFontOfSize:13] constrainedToSize:CGSizeMake(jieGuoAndPingYuView.frame.size.width - 30, 99999) lineBreakMode:NSLineBreakByCharWrapping];
        pingYuLabel.frame = CGRectMake(15, 43, jieGuoAndPingYuView.frame.size.width - 30, size.height);
    }
    [jieGuoAndPingYuView addSubview:pingYuLabel];

    
//---------------------时间和盖章----------------------
    UIView *dateAndSealView = [[UIView alloc]init];
    dateAndSealView.frame = CGRectMake(0, jieGuoAndPingYuView.frame.origin.y + jieGuoAndPingYuView.frame.size.height + space, kScreenWidth, dateAndSealViewHeight);
    [self.view addSubview:dateAndSealView];

    UILabel *dateLabel = [[UILabel alloc]init];
    if (kScreenWidth == 414) {
        dateLabel.frame = CGRectMake(kScreenWidth*(170.0/320.0), 50, 168, 36);
        dateLabel.font = [UIFont systemFontOfSize:15];
    }
    else{
        dateLabel.frame = CGRectMake(kScreenWidth*(170.0/320.0), 45, 140, 30);
        dateLabel.font = [UIFont systemFontOfSize:13];
    }
    dateLabel.backgroundColor = [UIColor clearColor];
    dateLabel.textAlignment = NSTextAlignmentCenter;
    dateLabel.textColor = [UIColor grayColor];
    
    NSDate *dates = [NSDate date];
    NSDateFormatter *formatter =  [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy年MM月dd日  HH:mm"];
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/beijing"];
    [formatter setTimeZone:timeZone];
    NSString *loctime = [formatter stringFromDate:dates];
    
    dateLabel.text = loctime;
    [dateAndSealView addSubview:dateLabel];
    
    UIImageView *sealView = [[UIImageView alloc]init];
    if (kScreenWidth == 414) {
        sealView.frame = CGRectMake(kScreenWidth*(233.0/320.0), 0, 80, 80);
    }
    else{
        sealView.frame = CGRectMake(kScreenWidth*(233.0/320.0), 0, 67, 67);
    }
    sealView.image = [UIImage imageNamed:@"ce_shi_bao_gao_seal"];
    [dateAndSealView addSubview:sealView];

    //分享
    UIButton *shareBTN = [[UIButton alloc]init];
    shareBTN.layer.cornerRadius = 3;
    if (kScreenWidth == 414) {
        shareBTN.frame = CGRectMake(20, dateAndSealView.frame.origin.y + dateAndSealView.frame.size.height + space, kScreenWidth - 40, shareBTNHeight);
    }
    else{
        shareBTN.frame = CGRectMake(15, dateAndSealView.frame.origin.y + dateAndSealView.frame.size.height + space, kScreenWidth - 30, shareBTNHeight);
    }
    [shareBTN setBackgroundImage:[UIImage imageNamed:@"tiJianFenXiangBG"] forState:UIControlStateNormal];
    [shareBTN setBackgroundImage:[UIImage imageNamed:@"tiJianFenXiangBG"] forState:UIControlStateHighlighted];
    [shareBTN setTitle:@"分享" forState:UIControlStateNormal];
    [shareBTN setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [shareBTN addTarget:self action:@selector(doToShare:) forControlEvents:UIControlEventTouchUpInside];
    if ([[UserManager shareInstance] getUserIsLogin].length > 0) {
        shareBTN.hidden = NO;
    }
    else{
        shareBTN.hidden = YES;
    }
    [self.view addSubview:shareBTN];
}

- (void)makeNavBar{
    UIBarButtonItem *backBtnImg = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"公用_返回.png"] style:UIBarButtonItemStylePlain target:self action:@selector(doBack)];
    [backBtnImg setTintColor:[UIColor whiteColor]];
    self.navigationItem.leftBarButtonItem = backBtnImg;
}

-(void)doBack{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)doToShare:(UIButton *)sender{
    CGRect rect = CGRectMake(0, 0, kScreenWidth, sender.frame.origin.y);
//    if (IPHONE5_OR_LATER) {
//        rect = CGRectMake(0, 0, 320, 395);
//    }
//    else{
//        rect = CGRectMake(0, 0, 320, 345);
//    }
    
    [self imageFromView:self.view atFrame:rect];
    
//分享
    [self showShareView];
}
#pragma 实现share方法，通过点击的视图的tag值判断是什么类型的分享
- (void)share:(UITapGestureRecognizer *)shareTap{
    switch (shareTap.view.tag) {
        case 11:{//微信
            [self weixinShare:kWeiXinShareUrl :kWeiXinShareTitle :kWeiXinShareText :_imageV];
        }
            break;
        case 12:{//朋友圈
            [self weixinFriendShare:kWeiXinFriendShareUrl :kWeiXinFriendShareTitle :kWeiXinFriendShareText :_imageV];
        }
            break;
        case 13:{//短信
            [self showSMSPicker:kWeiXinFriendShareUrl :kWeiXinFriendShareTitle :kWeiXinFriendShareText :nil];
        }
            break;
        case 14:{//QQ
            [self qqShare:kQQshareUrl :kQQshareTitle :kQQshreDescription :_imageV];
        }
            break;
        case 15:{//QQ空间
            [self qqZoneShare:kQQZoneShareUrl :kQQZoneShareTitle :kQQZoneShareDescription :_imageV];
        }
            break;
        case 16:{//新浪微博
            [self sinaWeiboShare:kSinaWeiboShareUrl :kSinaWeiboShareTitle :kSinaWeiboShareText :_imageV];
        }
            break;
            
        default:
            break;
    }
}


//- (void)imageFromView:(UIView *)theView atFrame:(CGRect)r
//{
//    UIGraphicsBeginImageContext(theView.frame.size);
//    CGContextRef context = UIGraphicsGetCurrentContext();
//    CGContextSaveGState(context);
//    UIRectClip(r);
//    [theView.layer renderInContext:context];
//    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
//    UIGraphicsEndImageContext();
//    
//    _imageV = theImage;
//
//    [self imageFromView:self.view atFrame:CGRectMake(0, 0, 320, 385+90)];
//
//    
//}


- (CGFloat)getPositionImageView_X{
    NSInteger currentTestResult = [[self.testResultDict valueForKey:kXinLvTestResult] integerValue];
    CGFloat totalWidth = levelLineImageView.frame.size.width;
    
    if (currentTestResult > 40 && currentTestResult <= 60) {
        return (CGFloat)((currentTestResult - 40.0)/20.0)*(totalWidth/3.0);
    }
    else if (currentTestResult > 60 && currentTestResult <= 100){
        return (CGFloat)((currentTestResult - 60.0)/40.0)*(totalWidth/3.0) + (totalWidth/3.0);
    }
    else if (currentTestResult > 100){
        return (CGFloat)((currentTestResult - 100.0)/60.0)*(totalWidth/3.0) + (totalWidth/3.0)*2;
    }
    return 0;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (void)fenxiang:(UIButton *)button{
    
}

- (void)imageFromView:(UIView *)theView atFrame:(CGRect)r
{
    UIGraphicsBeginImageContext(theView.frame.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [theView.layer renderInContext:context];
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    CGImageRef imageRef = theImage.CGImage;
    CGImageRef subImageRef = CGImageCreateWithImageInRect(imageRef, r);//原图中抠出来部分图
    UIImage *subImage = [UIImage imageWithCGImage:subImageRef];//新图
    _imageV = subImage;
    //    UIGraphicsBeginImageContext(theView.frame.size);
    //    CGContextRef context = UIGraphicsGetCurrentContext();
    //    CGContextSaveGState(context);
    //    UIRectClip(r);
    //    [theView.layer renderInContext:context];
    //    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    //    UIGraphicsEndImageContext();
    //
    //    _imageV = theImage;
    //    //已传过来值，从这里开始写测试报告
}

@end
